//
//  KKDataItemManager.m
//  Butler
//
//  Created by liaozhenming on 16/7/1.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKButlerDataManager.h"

@implementation KKButlerDataManager

+ (instancetype)kk_manager {
    
    static KKButlerDataManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [KKButlerDataManager new];
    });
    return manager;
}


- (instancetype)init {
    self = [super init];
    if (self) {
        
        _arr_categoryItems = [NSArray array];
        [self kk_updateAllCategoryItems: nil];
        [self kk_updateAllEmployeeItems: nil];
        [self kk_updateAllCustomerItems: nil];
        [self kk_searchAllGoodsItemsWithCategoryId: nil result: nil];
    }
    return self;
}


- (void)kk_updateAllCategoryItems:(KKButlerDataUpdateResultBlock)finishHandle {
    
    __weak typeof(self) weakSelf = self;
    [[KKDataBaseManager defaultManager] searchRecordWithTableName: KKDataBase_TableName_GoodsCategory withCondition: nil finishHandle:^(NSArray *object, BOOL successful) {
        NSMutableArray *itemArray = [NSMutableArray array];
        for (NSDictionary *itemDictionary in object) {
            KKCategoryItem *categoryItem = [KKCategoryItem new];
            [categoryItem kk_parserJsonDictionary: itemDictionary];
            [itemArray addObject: categoryItem];
        }
        weakSelf.arr_categoryItems = itemArray;
        
        if (finishHandle) {
            finishHandle(YES);
        }
    }];
}

- (void)kk_updateAllCustomerItems:(KKButlerDataUpdateResultBlock)finishHandle {
    
    __weak typeof(self) weakSelf = self;
    [[KKDataBaseManager defaultManager] searchRecordWithTableName: KKDataBase_TableName_CustomerInfo withCondition: nil finishHandle:^(NSArray *object, BOOL successful) {
        NSMutableArray *itemArray = [NSMutableArray array];
        for (NSDictionary *itemDictionary in object) {
            KKCustomer *categoryItem = [KKCustomer new];
            [categoryItem kk_parserJsonDictionary: itemDictionary];
            [itemArray addObject: categoryItem];
        }
        weakSelf.arr_customerItems = itemArray;
        if (finishHandle) {
            finishHandle(YES);
        }
    }];
}

- (void)kk_updateAllEmployeeItems:(KKButlerDataUpdateResultBlock)finishHandle {
    
    __weak typeof(self) weakSelf = self;
    [[KKDataBaseManager defaultManager] searchRecordWithTableName: KKDataBase_TableName_EmployeeInfo withCondition: nil finishHandle:^(NSArray *object, BOOL successful) {
        NSMutableArray *itemArray = [NSMutableArray array];
        for (NSDictionary *itemDictionary in object) {
            KKEmployee *categoryItem = [KKEmployee new];
            [categoryItem kk_parserJsonDictionary: itemDictionary];
            [itemArray addObject: categoryItem];
        }
        weakSelf.arr_employeeItems = itemArray;
        if (finishHandle) {
            finishHandle(YES);
        }
    }];
}


- (void)kk_searchAllGoodsItemsWithCategoryId:(NSString*)categoryId result:(KKButlerDataSearchDataBlock)resultHandle {
    
    if (categoryId.length == 0) {
        
        __weak typeof(self) weakSelf = self;
        [[KKDataBaseManager defaultManager] searchRecordWithTableName: KKDataBase_TableName_GoodsInfo withCondition: nil finishHandle:^(NSArray *object, BOOL successful) {
            NSMutableArray *itemArray = [NSMutableArray array];
            for (NSDictionary *itemDictionary in object) {
                KKGoodsItem *categoryItem = [KKGoodsItem new];
                [categoryItem kk_parserJsonDictionary: itemDictionary];
                [itemArray addObject: categoryItem];
            }
            weakSelf.arr_allGoodsItems = itemArray;
            if (resultHandle) {
                resultHandle(itemArray);
            }
        }];
    }
    else {
        
        [[KKDataBaseManager defaultManager] searchRecordWithTableName: KKDataBase_TableName_GoodsInfo withCondition: @{@"categoryId":categoryId} finishHandle:^(NSArray *object, BOOL successful) {
            NSMutableArray *itemArray = [NSMutableArray array];
            for (NSDictionary *itemDictionary in object) {
                KKGoodsItem *categoryItem = [KKGoodsItem new];
                [categoryItem kk_parserJsonDictionary: itemDictionary];
                [itemArray addObject: categoryItem];
            }
            
            if (resultHandle) {
                resultHandle(itemArray);
            }
        }];
    }
}

@end
